package com.qumitech.visualnovel.service.social.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.im.ChatGroupAdminRecordQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.im.ChatGroupForbidDTO;
import com.qumitech.visualnovel.pubdef.dto.im.ImChatGroupAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.im.ImUserChatGroupAdminQueryDTO;
import com.qumitech.visualnovel.service.social.SocialRelationService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: ywxie
 * @Date: 2019/7/12 17:13
 * @Description:
 */
@Service
public class SocialRelationServiceImpl implements SocialRelationService {

    private final String Social_Relation_Url = "/im/console/relation/apply/num";

    private final String CHAT_GROUP_URL = "/im/console/chatgroup";

    private final String CHAT_GROUP_MEMBER_URL = "/im/console/chatgroup/member";

    private final String CHAT_GROUP_FORBID = "/im/console/chatgroup/forbid";

    @Override
    public Map<String, Object> getUserRelationApplySum(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + Social_Relation_Url;
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public BaseRsp listChatGroup(ImChatGroupAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + CHAT_GROUP_URL;
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listUserChatGroup(ImUserChatGroupAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + CHAT_GROUP_MEMBER_URL;
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp listChatGroupAdminRecord(ChatGroupAdminRecordQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + CHAT_GROUP_FORBID;
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    @Override
    public BaseRsp forbidChatGroup(ChatGroupForbidDTO forbidDTO) {
        forbidDTO.setOperator(getActiveUserName());
        String url = UrlConstant.SERVER_URL + CHAT_GROUP_FORBID;
        String req = JSONObject.toJSONString(forbidDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        return JSONObject.parseObject(body, BaseRsp.class);
    }

    private String getActiveUserName(){
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }
}
